#include "sdlUtilitaires.h"
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>


void chargementErreur(sdlJeu * pSdlJeu, int erreur)
{
    switch(erreur)
    {
        case 1:
            if(pSdlJeu->surface_erreur!=NULL)  {SDL_FreeSurface(pSdlJeu->surface_erreur); pSdlJeu->surface_erreur=NULL;}
            pSdlJeu->surface_erreur=SDL_load_image("data/erreurs/temps_ecoule.bmp");
            if(pSdlJeu->surface_erreur==NULL) pSdlJeu->surface_erreur=SDL_load_image("../data/erreurs/temps_ecoule.bmp");
            assert(pSdlJeu->surface_erreur!=NULL);
        break;

        case 2:
            if(pSdlJeu->surface_erreur!=NULL)  {SDL_FreeSurface(pSdlJeu->surface_erreur); pSdlJeu->surface_erreur=NULL;}
             pSdlJeu->surface_erreur=SDL_load_image("data/erreurs/erreur_forme_mot.bmp");
             if(pSdlJeu->surface_erreur==NULL) pSdlJeu->surface_erreur=SDL_load_image("../data/erreurs/erreur_forme_mot.bmp");
             assert(pSdlJeu->surface_erreur!=NULL);
        break;

        case 3:
            if(pSdlJeu->surface_erreur!=NULL)  {SDL_FreeSurface(pSdlJeu->surface_erreur); pSdlJeu->surface_erreur=NULL;}
             pSdlJeu->surface_erreur=SDL_load_image("data/erreurs/erreur_centre.bmp");
             if(pSdlJeu->surface_erreur==NULL) pSdlJeu->surface_erreur=SDL_load_image("../data/erreurs/erreur_centre.bmp");
             assert(pSdlJeu->surface_erreur!=NULL);
        break;

        case 4:
            if(pSdlJeu->surface_erreur!=NULL)  {SDL_FreeSurface(pSdlJeu->surface_erreur); pSdlJeu->surface_erreur=NULL;}
            pSdlJeu->surface_erreur=SDL_load_image("data/erreurs/erreur_raccorde.bmp");
            if(pSdlJeu->surface_erreur==NULL) pSdlJeu->surface_erreur=SDL_load_image("../data/erreurs/erreur_raccorde.bmp");
            assert(pSdlJeu->surface_erreur!=NULL);
        break;

        case 5:
            if(pSdlJeu->surface_erreur!=NULL)  {SDL_FreeSurface(pSdlJeu->surface_erreur); pSdlJeu->surface_erreur=NULL;}
            pSdlJeu->surface_erreur=SDL_load_image("data/erreurs/mot.bmp");
            if(pSdlJeu->surface_erreur==NULL) pSdlJeu->surface_erreur=SDL_load_image("../data/erreurs/mot.bmp");
            assert(pSdlJeu->surface_erreur!=NULL);

        break;

    }
}

SDL_Surface *SDL_load_image(const char* filename )
{
	/* Temporary storage for the image that's loaded */
	SDL_Surface* loadedImage = NULL;

	/* The optimized image that will be used */
	SDL_Surface* optimizedImage = NULL;

	/* Load the image */
	loadedImage = SDL_LoadBMP( filename );

	/* If nothing went wrong in loading the image */
	if ( loadedImage != NULL )
	{
		/* Create an optimized image */
		optimizedImage = SDL_DisplayFormat( loadedImage );

		/* Free the old image */
		SDL_FreeSurface( loadedImage );
	}

	/* Return the optimized image */
	return optimizedImage;
}


void SDL_apply_surface( SDL_Surface* source, SDL_Surface* destination, int x, int y )
{
	/* Make a temporary rectangle to hold the offsets */
	SDL_Rect offset;

	/* Give the offsets to the rectangle */
	offset.x = x;
	offset.y = y;

	/* Blit the surface */
	SDL_BlitSurface( source, NULL, destination, &offset );
}
